import PyHook3 as pyHook
import pythoncom
import time,pymysql
import pyautogui as pag

db=pymysql.connect(host='192.168.1.62',user='root',password='luanshuo',db='test',port=6612)
cursor=db.cursor()

def onMouseEvent(event):   #监听到鼠标事件调用
    x, y = pag.position()  # 返回鼠标的坐标
    Chick_Right_Down, Chick_Right_Up, Chick_Left_Down, Chick_Left_Up, Wheel = "", "", "", "", ""
    if ("mouse left down" in event.MessageName):
        Chick_Left_Down="Y"
    elif ("mouse left up" in event.MessageName):
        Chick_Left_Up="Y"
    elif ("mouse right down" in event.MessageName):
        Chick_Right_Down="Y"
    elif ("mouse right up" in event.MessageName):
        Chick_Right_Up="Y"
    elif ("mouse wheel" in event.MessageName):
        Wheel="Y"
    cursor.execute("INSERT INTO label2 (Time,X,Y,Chick_Left_Down,Chick_Left_Up,Chick_Right_Down,Chick_Right_Up,Wheel) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s');"
                   % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), x, y, Chick_Left_Down,Chick_Left_Up,Chick_Right_Down,Chick_Right_Up,Wheel))
    db.commit()
    return True             # 为True才会正常调用，如果为False的话，此次事件被拦截

def main():
    hm = pyHook.HookManager()    # 创建管理器
    hm.MouseAll = onMouseEvent    # 监听鼠标
    hm.HookMouse()
    pythoncom.PumpMessages()    # 循环监听

if __name__ == "__main__":
    main()